package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);

        if (ids != null && ids.length > 0) {
            //处理中间表
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);


    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        //删除原本的关系
        employeeMapper.deleteRelation(employee.getId());
        if (ids != null && ids.length > 0) {
            //处理中间表
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public PageResult<Employee> query(QueryObject qo) {
        int count = employeeMapper.selectForCount(qo);
        int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > totalPage) {
            qo.setCurrentPage(totalPage);
        }
        if (count == 0) {
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), Collections.emptyList(), count);
        }
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), employees, count);

    }

    @Override
    public Employee login(String username, String password) {

        if (!StringUtils.hasLength(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (!StringUtils.hasLength(password)) {
            throw new RuntimeException("密码不能为空");
        }
        //查询数据库中是否有能匹配上的数据
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);

        if (employee == null) {
            throw new RuntimeException("用户名和密码不匹配");
        }

        return employee;
    }

    @Override
    public Employee updatePwd(Long id, String password) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        if (employee.getPassword() != password) {
            employee.setPassword(password);
            employeeMapper.updateByPrimaryKey(employee);
        }
        return employee;
    }

    @Override
    public Employee resetPwd(Long id, String newPassword) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        employee.setPassword(newPassword);
        employeeMapper.updateByPrimaryKey(employee);
        return employee;
    }
}
